﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Temp.View
{
    class Main
    {
        private int timer;
        private Texture2D curBg, main, boyOrGirl, choosePeer, start1, start2, start3, start4, help, helpTxt;
        private Texture2D credit;
        private Texture2D[] hps;

        private MouseState m;
        private Point mouse;                                                              //for touch input x and y
        private SpriteFont spFont;
        private String curStat, oldStat;                                                  //curStat or current user UI View        
        private bool clicked, charClicked, showStoryBox;                                                                       //implies that the mouse got clicked
        private bool update, returnPeer;                                                                        //update checker if the user changed places-> objects must get updated
        private String peer = "";

        private const int STARTX1 = 773;
        private const int STARTY1 = 334;
        private const int STARTX2 = 1126;
        private const int STARTY2 = 386;

        private const int HELPX1 = 781;
        private const int HELPY1 = 412;
        private const int HELPX2 = 1129;
        private const int HELPY2 = 461;

        private const int CREDX1 = 790;
        private const int CREDY1 = 482;
        private const int CREDX2 = 1116;
        private const int CREDY2 = 537;

        private const int EXITX1 = 812;
        private const int EXITY1 = 557;
        private const int EXITX2 = 1091;
        private const int EXITY2 = 602;

        private const int BOYX1 = 212;
        private const int BOYY1 = 155;
        private const int BOYX2 = 622;
        private const int BOYY2 = 707;

        private const int GIRLX1 = 729;
        private const int GIRLY1 = 153;
        private const int GIRLX2 = 1138;
        private const int GIRLY2 = 693;

        private const int PLAYX1 = 816;
        private const int PLAYY1 = 646;
        private const int PLAYX2 = 1070;
        private const int PLAYY2 = 725;

        private const int HPBACKX1 = 46;
        private const int HPBACKY1 = 699;
        private const int HPBACKX2 = 365;
        private const int HPBACKY2 = 743;

        private const int CBACKX1 = 764;
        private const int CBACKY1 = 590;
        private const int CBACKX2 = 1105;
        private const int CBACKY2 = 644;

        private int[] HPX1 = {38, 73, 38, 73, 73, 73, 38, 38, 38, 73};
        private int[] HPY1 = {123, 231, 262, 341, 380, 409, 450, 494, 553, 635};
        private const int HPX2 = 358;
        private int[] HPY2 = {222, 260, 340, 378, 410, 450, 498, 546, 633, 672};

        int hpCur=-1;
        public Main()
        {
            //IsMouseVisible = true;

            curStat = "main";
            oldStat = "";
            clicked = false;
            charClicked = false;
            showStoryBox = false;
            timer = 0;
            hps = new Texture2D[10];
            returnPeer = false;
        }

        public void LoadContent(ContentManager Content)
        {
            main = Content.Load<Texture2D>("UI elements/Main/main.png");
            boyOrGirl = Content.Load<Texture2D>("UI elements/Main/boyOrGirl.png");
            choosePeer = Content.Load<Texture2D>("UI elements/Main/choosePeer.png");
            start1 = Content.Load<Texture2D>("UI elements/Main/beforeStart/1.png");
            start2 = Content.Load<Texture2D>("UI elements/Main/beforeStart/2.png");
            start3 = Content.Load<Texture2D>("UI elements/Main/beforeStart/3.png");
            start4 = Content.Load<Texture2D>("UI elements/Main/beforeStart/4.png");
            help = Content.Load<Texture2D>("UI elements/Help/help.png");
            helpTxt = Content.Load<Texture2D>("UI elements/Help/helpTxt.png");
            credit = Content.Load<Texture2D>("UI elements/credits.png");
            for (int i = 0; i < 10; i++)
            {
                hps[i] = Content.Load<Texture2D>("UI elements/Main/help/"+(i+1)+".png");
            }
            curBg = main;                                                   //init start menu bg as the current bg
            spFont = Content.Load<SpriteFont>("UI elements/myFont");

        }

        public void Update(GameTime gameTime)
        {
            processMouse();
            if (timer >= 70)
                timer = 0;
            timer++;


        }

        public void Draw(SpriteBatch _spriteBatch)
        {
            if (curStat.Equals("main"))
                _spriteBatch.Draw(curBg, new Rectangle(0, 0, 1366, 768), Color.White);

            if (curStat.Equals("help") )
            {
                _spriteBatch.Draw(help, new Rectangle(0, 0, 1366, 768), Color.White);
                _spriteBatch.Draw(helpTxt, new Rectangle(50, 130, 307, 538), Color.White);

                if (hpCur!=-1)
                {
                    _spriteBatch.Draw(hps[hpCur], new Rectangle(375, 103, 946, 609), Color.White);
                }
                
            }
            if (curStat.Equals("credit"))
                _spriteBatch.Draw(credit, new Rectangle(0, 0, 1366, 768), Color.White);

            if (curStat.Equals("boyOrGirl"))
                _spriteBatch.Draw(boyOrGirl, new Rectangle(0, 0, 1366, 768), Color.White);

            if (curStat.Equals("peer"))
                _spriteBatch.Draw(choosePeer, new Rectangle(0, 0, 1366, 768), Color.White);

            if (oldStat.Equals("boy"))
            {
                if (curStat.Equals("boyPeer"))
                    _spriteBatch.Draw(start1, new Rectangle(0, 0, 1366, 768), Color.White);

                if (curStat.Equals("girlPeer"))
                    _spriteBatch.Draw(start3, new Rectangle(0, 0, 1366, 768), Color.White);
            }
            if (oldStat.Equals("girl"))
            {
                if (curStat.Equals("boyPeer"))
                    _spriteBatch.Draw(start2, new Rectangle(0, 0, 1366, 768), Color.White);

                if (curStat.Equals("girlPeer"))
                    _spriteBatch.Draw(start4, new Rectangle(0, 0, 1366, 768), Color.White);

            }
            //_spriteBatch.DrawString(spFont, m.X + ":" + m.Y, new Vector2(100, 100), Color.White);

           


        }
        /// <summary>
        /// returns the right gender of the peer at the right time
        /// </summary>
        /// <param name="b"></param>
        /// <returns>gender of the peer</returns>
        public String getPeer()
        {
            return peer;
        }
        protected void processMouse()
        {
            m = Mouse.GetState();
            if(curStat.Equals("main"))
                hpCur=-1;
            if (m.LeftButton == ButtonState.Pressed)
            {
                mouse.X = m.X;
                mouse.Y = m.Y;

                if (curStat.Equals("girlPeer") || curStat.Equals("boyPeer"))
                {
                    if (mouse.X >= PLAYX1 && mouse.X < PLAYX2 && mouse.Y >= PLAYY1 && mouse.Y < PLAYY2)
                    {
                        if (curStat.Equals("girlPeer"))
                            peer = "Girl";
                        if (curStat.Equals("boyPeer"))
                            peer = "Boy"; 
                        curStat = "play";
                        Mouse.SetPosition(0, 0);
                    }
                }
                if (curStat.Equals("peer"))
                {
                    if (mouse.X >= BOYX1 && mouse.X < BOYX2 && mouse.Y >= BOYY1 && mouse.Y < BOYY2)
                    {
                        Mouse.SetPosition(0, 0);
                        curStat = "boyPeer";
                    }
                    if (mouse.X >= GIRLX1 && mouse.X < GIRLX2 && mouse.Y >= GIRLY1 && mouse.Y < GIRLY2)
                    {
                        Mouse.SetPosition(0, 0);
                        curStat = "girlPeer";

                    }
                }
                if (curStat.Equals("boyOrGirl"))
                {
                    if (mouse.X >= BOYX1 && mouse.X < BOYX2 && mouse.Y >= BOYY1 && mouse.Y < BOYY2)
                    {
                        Mouse.SetPosition(0, 0);
                        curStat = "peer";
                        oldStat = "boy";
                    }
                    if (mouse.X >= GIRLX1 && mouse.X < GIRLX2 && mouse.Y >= GIRLY1 && mouse.Y < GIRLY2)
                    {
                        Mouse.SetPosition(0, 0);
                        curStat = "peer";
                        oldStat = "girl";
                    }
                }

                if (curStat.Equals("credit"))
                {
                    if (mouse.X >= CBACKX1 && mouse.X < CBACKX2 && mouse.Y >= CBACKY1 && mouse.Y < CBACKY2)
                    {
                        Mouse.SetPosition(0, 0);
                        curStat = "main";
                    }

                }
                if (curStat.Equals("help"))
                {
                    if (mouse.X >= HPBACKX1 && mouse.X < HPBACKX2 && mouse.Y >= HPBACKY1 && mouse.Y < HPBACKY2)
                    {
                        Mouse.SetPosition(0, 0);
                        curStat = "main";
                    }
                    for (int i = 0; i < 10; i++)
                    {
                        if (mouse.X >= HPX1[i] && mouse.X < HPX2&& mouse.Y >= HPY1[i] && mouse.Y < HPY2[i])
                        { 
                            hpCur = i ;
                            System.Diagnostics.Debug.WriteLine(i+1);
                        }
                    }


                }
                if (curStat.Equals("main"))
                {
                    if (mouse.X >= STARTX1 && mouse.X < STARTX2 && mouse.Y >= STARTY1 && mouse.Y < STARTY2)
                    {
                        Mouse.SetPosition(0, 0);
                        curStat = "boyOrGirl";
                    }
                    if (mouse.X >= HELPX1 && mouse.X < HELPX2 && mouse.Y >= HELPY1 && mouse.Y < HELPY2)
                    {
                        Mouse.SetPosition(0, 0);
                        curStat = "help";
                    }
                    if (mouse.X >= CREDX1 && mouse.X < CREDX2 && mouse.Y >= CREDY1 && mouse.Y < CREDY2)
                    {
                        Mouse.SetPosition(0, 0);
                        curStat = "credit";
                    }
                    //if (mouse.X >= EXITX1 && mouse.X < EXITX2 && mouse.Y >= EXITY1 && mouse.Y < EXITY2)
                    //{
                        
                    //}
                    
                }


            }
        }
    }
}
